package com.hakgc.rent.entity;

import com.hakgc.rent.constant.SystemConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * HttpSessionBindingListener
 * 实现该接口表明，
 * 当前类的对象被放入HttpSession作用域时会触发，valueBind()方法
 * 从作用域中移除时，会触发valueUnbound()方法
 *
 * 触发条件，必须是手动的session.setAttribute()及session.removeAttribute()
 *
 * @author Xing.Jiejian
 * @date 2020/11/17
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements HttpSessionBindingListener {
    private Integer id;
    private String name;
    private String password;
    private String logoUrl;

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        // 系统在线人数嘉义
        SystemConstant.ON_LINE_USER++;
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        SystemConstant.ON_LINE_USER--;
    }
}
